package com.zm.tools.util.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * @author zoum
 * @create 2018/6/11 10:57
 */
public class PropertiesConfigUtil extends PropertiesLoaderUtils {

    private List<Resource> resources;

    private static Properties properties = new Properties();

    private static Logger LOGGER = LoggerFactory.getLogger(PropertiesConfigUtil.class);

    public void init(){
        if (!CollectionUtils.isEmpty(resources)){
            for (Resource resource : resources){
                try {
                    fillProperties(properties, resource);
                } catch (IOException ie){
                    LOGGER.error("PropertiesLoaderUtil init error. ", ie);
                }
            }
        }
    }

    public static String getPropByKey(String key){
        return properties.getProperty(key);
    }

    public static String getPropByKey(String key, String defaultValue){
        return properties.getProperty(key, defaultValue);
    }

    public List<Resource> getResources() {
        return resources;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }
}
